package com.ruoyi.generator.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.generator.domain.FromCrudSuperQuery;
import com.ruoyi.generator.service.IFromCrudSuperQueryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 单设计__模块高级搜索配置Controller
 *
 * @author ZGL
 * @date 2025-04-01
 */
@RestController
@RequestMapping("/generator/superQuery")
public class FromCrudSuperQueryController extends BaseController
{
    @Autowired
    private IFromCrudSuperQueryService crudSuperQueryService;

    /**
     * 保存CRUD高级查询
     * @return
     */
    @PostMapping("/save")
    public AjaxResult saveSuperQuery(@RequestBody FromCrudSuperQuery fromCrudSuperQuery) throws Exception {
        if(fromCrudSuperQuery == null){
            return AjaxResult.error("操作失败，参数信息不能为空");
        }
        //获取平台用户信息
        LoginUser loginUser = SecurityUtils.getLoginUser();
        fromCrudSuperQuery.setCreateBy(loginUser.getUsername());
        int exeCount = 0;
        if(fromCrudSuperQuery.getId()!=null){
            exeCount = crudSuperQueryService.updateFromCrudSuperQuery(fromCrudSuperQuery);
        }else{
            exeCount = crudSuperQueryService.insertFromCrudSuperQuery(fromCrudSuperQuery);
        }
        return toAjax(exeCount);
    }

    /**
     * 获取CRUD高级查询信息
     * @param buildKey
     * @return
     * @throws Exception
     */
    @GetMapping("/list/{buildKey}")
    public AjaxResult listSuperQuery(@PathVariable("buildKey") String buildKey) throws Exception {
        if(StringUtils.isEmptyIncludeNull(buildKey)){
            return AjaxResult.success();
        }
        //获取平台用户信息
        LoginUser loginUser = SecurityUtils.getLoginUser();
        FromCrudSuperQuery superQuery =new FromCrudSuperQuery();
        superQuery.setBuildKey(buildKey);
        superQuery.setCreateBy(loginUser.getUsername());
        return AjaxResult.success(crudSuperQueryService.selectFromCrudSuperQueryList(superQuery));
    }

    /**
     * 删除单设计__模块高级搜索配置
     */
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(crudSuperQueryService.deleteFromCrudSuperQueryById(id));
    }
}
